//
//  EpointContactsDetailViewController.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-2-13.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointContactsDetailViewController.h"
#import "EpointUIButtonBindingValue.h"

@interface EpointContactsDetailViewController ()
{
    UITableView *tv;
    NSMutableDictionary *userDetail;
    NSString *UserGuid;
    UIImageView *headImageView;
}
@end

@implementation EpointContactsDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    tv = [[UITableView alloc]initWithFrame:[self getFullContentFrame] style:UITableViewStyleGrouped];
    tv.delegate = self;
    tv.dataSource = self;

    tv.backgroundView = nil;
    tv.backgroundColor = [UIColor clearColor];
    [self.view setBackgroundColor:COLOR(239, 239, 243)];
    
    UserGuid = [[NSString alloc]initWithString:[self.passParam objectForKey:@"userguid"]];
    userDetail = [DBFrameService getUserDetailByUserguid:UserGuid];
    
    [self.view addSubview:tv];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:UserGuid forKey:@"userguid"];
    [self startTask:@"EpointLoadUserHeadImageTask" withParams:params withTaskId:3];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 7;
            break;
        case 2:
            return 2;
            break;
        default:
            return 0;
            break;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return @"";
            break;
        case 1:
            return @"信息";
        case 2:
            return @"操作";
        default:
            return @"";
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int section = [indexPath indexAtPosition:0];
    if (section==0)
    {
        return 120;
    }
    else if (section==1)
    {
        return 50;
    }
    else if (section==2)
    {
        return 50;
    }
    return 0;
}

#pragma 获取用户头像
-(void)getUserHeadImage
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:[DBFrameService getUserGuid] forKey:[self.passParam objectForKey:@"userguid"]];
    [self startTask:@"EpointLoadUserHeadImageTask" withParams:params withTaskId:3];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"simplecell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]init];
    }
    
    int section = [indexPath indexAtPosition:0];
    if (section==0)
    {
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//        cell.backgroundColor = [UIColor clearColor];
        
        CGFloat img_len = 100;
        
        CGFloat hX = (AppWidth-img_len)/2;
        if (!ISIOS7&&CurrentDeviceISiPad) {
            hX = (AppWidth-img_len-84)/2;
        }
        else if (!ISIOS7&&!CurrentDeviceISiPad) {
            hX = (AppWidth-img_len-14)/2;
        }
        
        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(hX, 10, img_len, img_len)];
        iv.contentMode = UIViewContentModeScaleAspectFit;
        
        if ([[userDetail objectForKey:@"sex"]isEqualToString:@"男"]) {
            [iv setImage:[UIImage imageNamed:@"img_man"]];
        }
        else
        {
            [iv setImage:[UIImage imageNamed:@"img_woman"]];
        }
        
        NSString *dirPath = [NSString stringWithFormat:@"%@/Documents/photos/%@.jpg",NSHomeDirectory(),UserGuid];
        
        NSDictionary* dictFile = [[NSFileManager defaultManager] attributesOfItemAtPath:dirPath error:nil];
        
        NSFileManager *fm = [NSFileManager defaultManager];
        
        if ([fm fileExistsAtPath:dirPath isDirectory:NO]&&dictFile>0) {
            NSData *tmpContent = [fm contentsAtPath:dirPath];
            [iv setImage:[UIImage imageWithData:tmpContent]];
            headImageView = iv;
        }
        
        iv.layer.masksToBounds = YES;
        iv.layer.cornerRadius = img_len/2;
        [cell.contentView addSubview:iv];
    }
    else if (section==1)
    {
        UILabel *lbTip = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, AppWidth/4, 50)];
        lbTip.textAlignment = NSTextAlignmentCenter;
        lbTip.backgroundColor = [UIColor clearColor];
        lbTip.textColor = COLOR(102, 102, 255);
        UILabel *lbDetail = [[UILabel alloc]initWithFrame:CGRectMake(AppWidth/4, 0, AppWidth*3/4-110, 50)];
        lbDetail.backgroundColor = [UIColor clearColor];
        if (indexPath.row==0) {
            lbTip.text = @"部门";
            lbDetail.frame =CGRectMake(AppWidth/4, 0, AppWidth*3/4, 50);
            lbDetail.text = [userDetail objectForKey:@"ouname"];
        }
        else if(indexPath.row==1) {
            lbTip.text = @"职位";
            lbDetail.frame =CGRectMake(AppWidth/4, 0, AppWidth*3/4, 50);
            lbDetail.text = [userDetail objectForKey:@"title"];
        }
        else if(indexPath.row==2) {
            lbTip.text = @"性别";
            lbDetail.text = [userDetail objectForKey:@"sex"];
        }
        else if(indexPath.row==3) {
            [self setPhoneControl:cell labelTip:lbTip labelDetail:lbDetail strTipName:@"手机" strValue:[userDetail objectForKey:@"mobile"]];
        }
        else if(indexPath.row==4) {
            [self setPhoneControl:cell labelTip:lbTip labelDetail:lbDetail strTipName:@"办公" strValue:[userDetail objectForKey:@"teleoffice"]];
        }
        else if(indexPath.row==5) {
            [self setPhoneControl:cell labelTip:lbTip labelDetail:lbDetail strTipName:@"家庭" strValue:[userDetail objectForKey:@"telehome"]];
        }
        else if(indexPath.row==6) {
            lbTip.text = @"邮箱";
            lbDetail.frame =CGRectMake(AppWidth/4, 0, AppWidth*3/4, 50);
            lbDetail.text = [userDetail objectForKey:@"mail"];
        }
        [cell.contentView addSubview:lbTip];
        [cell.contentView addSubview:lbDetail];
    }
    else if (section==2)
    {
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor redColor];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        if (indexPath.row==0) {
            cell.textLabel.text = @"同步个人数据";
        }
        else if (indexPath.row==1)
        {
            cell.textLabel.text = @"发送OA邮件";
        }
    }
    return cell;
}

-(void)setPhoneControl:(UITableViewCell *)cell labelTip:(UILabel *)lbTip labelDetail:(UILabel *)lbDetail strTipName:(NSString *)tipName strValue:(NSString *)value
{
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    lbTip.text = tipName;
    lbDetail.text = value;
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:value forKey:@"num"];
    if (lbDetail.text.length>0) {
        
        CGFloat msgX =lbDetail.frame.origin.x+lbDetail.frame.size.width;
        if (CurrentDeviceISiPad&&!ISIOS7) {
            msgX -= 80;
        }
        
        EpointUIButtonBindingValue *btMsg = [[EpointUIButtonBindingValue alloc]initWithFrame:CGRectMake(msgX, 0, 50, 50)];
        [btMsg setPassValue:dic];
        [btMsg setImage:[UIImage imageNamed:@"img_smsg_a"] forState:UIControlStateNormal];
        [btMsg addTarget:self action:@selector(sendmsg:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btMsg];
        
        EpointUIButtonBindingValue *btCall = [[EpointUIButtonBindingValue alloc]initWithFrame:CGRectMake(btMsg.frame.origin.x+btMsg.frame.size.width, 0, 50, 50)];
        [btCall setPassValue:dic];
        [btCall setImage:[UIImage imageNamed:@"img_call_a"] forState:UIControlStateNormal];
        [btCall addTarget:self action:@selector(call:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btCall];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int section = [indexPath indexAtPosition:0];
    if (section==1&&indexPath.row==6) {
        NSString *mail = [userDetail objectForKey:@"mail"];
        if (mail.length>0) {
            [self sendMail:[userDetail objectForKey:@"mail"]];
        }
        
    }
}

-(void)sendmsg:(EpointUIButtonBindingValue *)sender
{
    NSString *numFormat = [NSString stringWithFormat:@"sms://%@",[sender.passValue objectForKey:@"num"]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numFormat]];
}

-(void)call:(EpointUIButtonBindingValue *)sender
{
    NSString *numFormat = [NSString stringWithFormat:@"tel://%@",[sender.passValue objectForKey:@"num"]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numFormat]];
}

-(void)sendMail:(NSString *)mail
{
    NSString *numFormat = [NSString stringWithFormat:@"mailto:%@",mail];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numFormat]];
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    if (taskId==3) {
        [tv reloadData];
    }
}

@end
